import time

import pymysql
import unittest

from Base.DataCollect import DataCollect


from pymysql.cursors import SSDictCursor

from Base.Report_Result import TestResult
from Business.all import WoniuBoss
from TestCase import testcase_sun, ClassManagement_wxj, All_Managemaent
from TestCase.All_Managemaent import Us

class info_collect():

    @classmethod
    def get_data(cls):

        cls.suite = unittest.TestLoader().loadTestsFromModule(All_Managemaent)
        # cls.suite = unittest.TestLoader().loadTestsFromModule(ClassManagement_wxj)
        runner = unittest.TextTestRunner(resultclass=TestResult)  # 使用定制的TestResult类
        result = runner.run(cls.suite)
        results = result.summary
        print(results)

        for detail in results.get('details'):
            detail_info_dict = {}
            detail_info_dict['case_id'] = detail.get('name')
            detail_info_dict['autual_res'] = detail.get('status')
            detail_info_dict['start_time'] = detail.get('time').get('start_at')
            detail_info_dict['end_time'] = detail.get('time').get('end_at')
            detail_info_dict['duration_time'] = detail.get('time').get('duration')
            detail_info_dict['fail_info'] = detail.get('reason')
            if detail_info_dict['fail_info'] is not None:
                detail_info_dict['fail_info'] = detail.get('reason').split('\n')[0]
            DataCollect.case_detail_list.append(detail_info_dict)


        stat_dict = results.get('stat')
        time_dict = results.get('time')

        DataCollect.execute_sum = stat_dict.get('testsRun')
        # print(DataCollect.execute_sum)
        DataCollect.success_num = stat_dict.get('successes')
        # print(DataCollect.success_num)
        DataCollect.fail_sum = stat_dict.get('failures')
        DataCollect.percent = str(round(DataCollect.success_num / DataCollect.execute_sum,4) * 100)+'%'


        DataCollect.test_start_time = time_dict.get('start_at')
        DataCollect.test_end_time = time_dict.get('end_at')
        DataCollect.test_duration_time = time_dict.get('duration')
        DataCollect.Batch_number = 'WN'+ str(round(time.time()))

        result = (DataCollect.execute_sum,DataCollect.success_num,DataCollect.fail_sum,DataCollect.percent,DataCollect.test_start_time,DataCollect.test_end_time,DataCollect.test_duration_time,DataCollect.Batch_number, DataCollect.case_detail_list)
        return result

if __name__ == '__main__':
    # get_data()
    a = info_collect()
    a.get_data()